package com.ruicar.afs.cloud.workflow.greatwall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.workflow.api.enums.FlowStatus;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallWorkflowBizData;
import com.ruicar.afs.cloud.workflow.greatwall.mapper.AfsGreatWallTaskAssignInfoMapper;
import com.ruicar.afs.cloud.workflow.greatwall.mapper.AfsGreatWallTaskInfoMapper;
import com.ruicar.afs.cloud.workflow.greatwall.mapper.AfsGreatWallWorkflowBizDataMapper;
import com.ruicar.afs.cloud.workflow.greatwall.mapper.AfsGreatWallWorkflowNoticeExceptionMapper;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowBizDataService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class AfsGreatWallWorkflowBizDataServiceImpl extends ServiceImpl<AfsGreatWallWorkflowBizDataMapper, AfsGreatWallWorkflowBizData> implements AfsGreatWallWorkflowBizDataService {
    private final AfsGreatWallTaskInfoMapper afsGreatWallTaskInfoMapper;
    private final AfsGreatWallTaskAssignInfoMapper afsGreatWallTaskAssignInfoMapper;
    private final AfsGreatWallWorkflowNoticeExceptionMapper afsGreatWallWorkflowNoticeExceptionMapper;

    @Override
    public String getBizDataByServiceIdAndDataId(String serviceId, String bizId) {
        AfsGreatWallWorkflowBizData afsGreatWallWorkflowBizData = this.baseMapper.selectOne(Wrappers.<AfsGreatWallWorkflowBizData>lambdaQuery().eq(AfsGreatWallWorkflowBizData::getFlowServiceId, serviceId).eq(AfsGreatWallWorkflowBizData::getBizDataId, bizId));
        return afsGreatWallWorkflowBizData == null ? null : afsGreatWallWorkflowBizData.getBizData();
    }

    @Override
    public AfsGreatWallWorkflowBizData getBizDataObjServiceIdAndDataId(String serviceId, String bizId) {
        return this.baseMapper.selectOne(Wrappers.<AfsGreatWallWorkflowBizData>lambdaQuery().eq(AfsGreatWallWorkflowBizData::getFlowServiceId, serviceId).eq(AfsGreatWallWorkflowBizData::getBizDataId, bizId));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateBizData(String serviceId, String bizId, String serviceName, String bizData) {
        AfsGreatWallWorkflowBizData afsGreatWallWorkflowBizData = this.baseMapper.selectOne(Wrappers.<AfsGreatWallWorkflowBizData>lambdaQuery().eq(AfsGreatWallWorkflowBizData::getFlowServiceId, serviceId).eq(AfsGreatWallWorkflowBizData::getBizDataId, bizId).eq(AfsGreatWallWorkflowBizData::getAfsServiceName, serviceName));
        if (afsGreatWallWorkflowBizData != null) {
            this.baseMapper.updateById(afsGreatWallWorkflowBizData);
        }
    }

    @Override
    public AfsGreatWallWorkflowBizData getBizDataObjServiceIdAndDataId(String serviceId, String bizId, String serviceName) {
        return this.baseMapper.selectOne(Wrappers.<AfsGreatWallWorkflowBizData>lambdaQuery().eq(AfsGreatWallWorkflowBizData::getFlowServiceId, serviceId).eq(AfsGreatWallWorkflowBizData::getBizDataId, bizId).eq(AfsGreatWallWorkflowBizData::getAfsServiceName, serviceName));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveBizDate(AfsGreatWallWorkflowBizData afsGreatWallWorkflowBizData) {
        this.baseMapper.insert(afsGreatWallWorkflowBizData);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean exception(String serviceId, String bizDataId) {
        int count = this.count(
                Wrappers
                        .<AfsGreatWallWorkflowBizData>lambdaQuery()
                        .eq(AfsGreatWallWorkflowBizData::getFlowServiceId, serviceId)
                        .eq(AfsGreatWallWorkflowBizData::getBizDataId, bizDataId)
                        .eq(AfsGreatWallWorkflowBizData::getFlowStatus, FlowStatus.ERROR)
        );
        return count > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void setFinish(Long id) {
        this.baseMapper.updateToFinish(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearStartData(AfsGreatWallWorkflowBizData afsGreatWallWorkflowBizData) {
        this.baseMapper.clearFailInfo(afsGreatWallWorkflowBizData.getFlowServiceId(), afsGreatWallWorkflowBizData.getBizDataId());
        this.afsGreatWallTaskInfoMapper.clearFailInfo(afsGreatWallWorkflowBizData.getFlowServiceId(), afsGreatWallWorkflowBizData.getBizDataId());
        this.afsGreatWallTaskAssignInfoMapper.clearFailInfo(afsGreatWallWorkflowBizData.getFlowServiceId(), afsGreatWallWorkflowBizData.getBizDataId());
        this.afsGreatWallWorkflowNoticeExceptionMapper.clearFailInfo(afsGreatWallWorkflowBizData.getFlowServiceId(), afsGreatWallWorkflowBizData.getBizDataId());
    }

    @Override
    public String queryLastAssignInfo(String serviceId, String bizId) {
        String assignName = this.afsGreatWallTaskAssignInfoMapper.queryLastAssignUser(serviceId,bizId);
        return StringUtils.isEmpty(assignName)?"":assignName;
    }
}
